﻿using System;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;
using JumpstartApp.ViewModels;
using Windows.UI.StartScreen;


namespace JumpstartApp.Views
{
    using JumpstartApp.ViewModels;

    using JumpstartApp.Helpers;

    public sealed partial class Detail : Helpers.LayoutAwarePage
    {
        private DispatcherTimer appbarTimer;

        private DetailViewModel detailViewModel;

        public Detail()
        {
            this.InitializeComponent();
            this.Loaded += this.OnLoaded;
        }

        #region Navigation
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (e.Parameter == null)
                return;

            this.detailViewModel = (this.DataContext as ViewModels.DetailViewModel);

            await this.detailViewModel.Start(this, PrintContainer, DetailAppBar, e.Parameter.ToString());

            this.SetupPlayTo();

            await this.UpdateSilhouette();

        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            Windows.Media.PlayTo.PlayToManager.GetForCurrentView().SourceRequested -= ModuleDetail_SourceRequested;

            this.detailViewModel.Stop();
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            this.SetupAppBarTimer();
        }

        #endregion 

        #region Silhouette
        private async Task UpdateSilhouette()
        {
            // UpdateSilhouette
            var _UpdateSilhouette =
                new Action(
                    async () =>
                        {
                            await
                                this.Dispatcher.RunAsync(
                                    Windows.UI.Core.CoreDispatcherPriority.Normal,
                                    () => { this.MySilhouette.ShowGuide = Contracts.Settings.ShowSilhouette; });
                        });
            ApplicationData.Current.DataChanged += (s, args) => _UpdateSilhouette();
            _UpdateSilhouette();
        }
        #endregion

        #region PlayTo
        private void SetupPlayTo()
        {
            // playto
            Windows.Media.PlayTo.PlayToManager.GetForCurrentView().SourceRequested += this.ModuleDetail_SourceRequested;
            this.ApplicationViewStates.CurrentStateChanged += (s, args) =>
            {
                this.MyMediaPlayerLandscape.Stop();
                this.MyMediaPlayerPortrait.Stop();
            };
        }

        void ModuleDetail_SourceRequested(
            Windows.Media.PlayTo.PlayToManager sender, 
            Windows.Media.PlayTo.PlayToSourceRequestedEventArgs args)
        {
            var _Deferral = args.SourceRequest.GetDeferral();
            var _Handler = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (ApplicationViewStates.CurrentState == FullScreenPortrait)
                    args.SourceRequest.SetSource(MyMediaPlayerPortrait.PlayToSource);
                else
                    args.SourceRequest.SetSource(MyMediaPlayerLandscape.PlayToSource);
                _Deferral.Complete();
            });
        }

        private void PlaySnappedButton_Click_1(object sender, RoutedEventArgs e)
        {
            Windows.UI.ViewManagement.ApplicationView.TryUnsnap();
        }

        #endregion

        #region Hide AppBar after a few seconds

        private void SetupAppBarTimer()
        {
            this.appbarTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(3) };
            this.appbarTimer.Tick += this.HideAppBar;
            this.appbarTimer.Start();
        }

        private void HideAppBar(object sender, object e)
        {
            if (DetailAppBar.IsOpen)
            {
                DetailAppBar.IsOpen = false;
            }
            appbarTimer.Stop();
            appbarTimer = null;
        }
        #endregion
    }
}
